package com.lly.bigdata.bgayx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lly.bigdata.bgayx.common.utils.IdUtils;
import com.lly.bigdata.bgayx.entity.SysPost;
import com.lly.bigdata.bgayx.entity.SysUser;
import com.lly.bigdata.bgayx.mapper.SysPostMapper;
import com.lly.bigdata.bgayx.service.ISysPostService;
import com.lly.bigdata.bgayx.vo.Result;
import com.lly.bigdata.bgayx.vo.SearchModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 岗位 服务实现类
 * </p>
 *
 * @author lly
 * @since 2020-10-21
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {


    /**
     * @return Result
     * @Author lly
     * @Description 查询分页岗位数据
     * @Date 2021/8/6 14:34
     * @Param [searchModel]
     **/
    @Override
    public Result queryPost(SearchModel searchModel) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(searchModel.getS_name())) {
            queryWrapper.like("post_name", searchModel.getS_name());
        }
        queryWrapper.orderByDesc(" post_sort ");
        IPage page = new Page(searchModel.getPageNumber(), searchModel.getPageSize());
        IPage<SysPost> pages = this.baseMapper.selectPage(page, queryWrapper);
        Result res = Result.pageOk(page.getRecords());
        res.setTotal(pages.getTotal());
        res.setPages((long) page.getPages());
        return res;
    }

    /**
     * @return Result
     * @Author lly
     * @Description 保存或更新
     * @Date 2022/1/23 18:02
     * @Param [sysPost, sysUser]
     **/
    @Override
    public Result saveOrUpdate(SysPost sysPost, SysUser sysUser) {
        if (StringUtils.isEmpty(sysPost.getId())) {
            sysPost.setId(IdUtils.simpleUUID());
            sysPost.setCreateTime(LocalDateTime.now());
            sysPost.setCreateBy(sysUser.getUsername());
            sysPost.setStatus(1);
        } else {
            SysPost byId = this.getById(sysPost.getId());
            byId.setPostName(sysPost.getPostName());
            byId.setPostCode(sysPost.getPostCode());
            byId.setPostSort(sysPost.getPostSort());
            byId.setRemark(sysPost.getRemark());
            sysPost = byId;
        }
        this.saveOrUpdate(sysPost);
        return Result.ok();
    }

}
